/**
 * DEVICE
 * @return {String} DEVICE
 */
export const DEVICE = (() => {
  const ua = navigator.userAgent.toLowerCase()
  const is = {
    ios: ua.indexOf('hero-ios') > 0,
    android: ua.indexOf('hero-android') > 0,
    wechat: ua.indexOf('micromessenger') > 0
  }
  const str = (() => {
    if (is.ios) return 'ios'
    if (is.android) return 'android'
    if (is.wechat) return 'wechat'
    return 'unknow'
  })()
  return {
    ...is,
    toString: () => str
  }
})()

/**
 * callNative
 * @return {Promise}
 */
export const callNative = (() => {
  // @type {Function}
  let sender = () => {}
  if (DEVICE.ios) {
    sender = (data) => window.jsBridge.send(data)
  }
  if (DEVICE.android) {
    sender = (data) => window.native.on(data)
  }

  return function (data) {
    return Promise.resolve(sender(JSON.stringify(data)))
  }
})()

callNative.command = (command) => callNative({ command })
callNative.global = (global) => callNative({ global })

